package ve.basura.entrada.main;

import org.apache.commons.logging.Log;
import org.eclipse.swt.SWT;
import org.eclipse.swt.graphics.GC;
import org.eclipse.swt.graphics.Image;

import org.eclipse.swt.layout.FormAttachment;
import org.eclipse.swt.layout.FormData;
import org.eclipse.swt.layout.FormLayout;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.ProgressBar;
import org.eclipse.swt.widgets.Shell;

import ve.basura.entrada.core.SWTCore;
import ve.basura.entrada.util.ApplicationContextUtil;
import ve.basura.entrada.util.EntradaSalidaUtil;

public class EntradaMain {
	
	public EntradaMain(){}
	
	public EntradaMain(Display display, final EntradaSalidaUtil entradaSalidaUtil,Log appLogger){
		
		//EntradaSalidaUtil entradaSalidaUtil =(EntradaSalidaUtil)ctxt.getBean("entradaSalidaUtil");
		final Shell splash = new Shell(SWT.ON_TOP);//shell que visualiza el splash
		final Image image = new Image(display, 530, 530);//imagen fondo nulo para el splash
		
		appLogger.info(entradaSalidaUtil.getImagesMap());
		//modificamos la imagen recien creada
		Image imgsplash = entradaSalidaUtil.getImagesMap().get("splashImage");
		GC gc = new GC(image);
		gc.drawText(entradaSalidaUtil.getProperty("entradaMain.aplicacionEntradaLabel"), 5, 5);
		gc.drawImage(imgsplash, 5, 5);
		gc.dispose();
		
		//a�adimos progress
		final ProgressBar bar = new ProgressBar(splash, SWT.NONE);
		bar.setMaximum(3);
		FormData progressData = new FormData ();
		progressData.left = new FormAttachment (0, 5);
		progressData.right = new FormAttachment (100, -5);
		progressData.bottom = new FormAttachment (100, -5);
		bar.setLayoutData(progressData);
		
		//a�adimos splash 
		Label label = new Label(splash, SWT.NONE);
		label.setImage(image);
		FormData labelData = new FormData ();
		labelData.right = new FormAttachment (100, 0);
		label.setLayoutData(labelData);
	
		//a�adimos label configurable	
		final Label labelStatus = new Label(splash, SWT.NONE);
		labelStatus.setText(entradaSalidaUtil.getProperty("entradaMain.cargandoComponentesLabel"));
		labelStatus.setVisible(true);
		FormData labelStatusData = new FormData ();
		labelStatusData.top= new FormAttachment (label, 5);
		labelStatusData.left= new FormAttachment (0, 5);
		labelStatusData.right= new FormAttachment(100,0);
		labelStatusData.bottom = new FormAttachment (bar, 0);
		labelStatus.setLayoutData(labelStatusData);

		FormLayout layout = new FormLayout();
		splash.setLayout(layout);
		splash.pack();
		SWTCore.setLocation(splash, display);
	
		/*splash.open();
		
		display.syncExec(new Runnable() {
			public void run() {
				bar.setSelection(1);
				try {Thread.sleep(2000);} catch (Throwable e) {}
				
				bar.setSelection(2);
				labelStatus.setText(entradaSalidaUtil.getProperty("entradaMain.verificandoComponentesLabel"));
				try {Thread.sleep(2000);} catch (Throwable e) {}
				 
				labelStatus.setText(entradaSalidaUtil.getProperty("entradaMain.inicializandoWidgets"));
				bar.setSelection(3);
				try {Thread.sleep(2000);} catch (Throwable e) {}
				
				splash.close();
				image.dispose();
			}
		});		
		*/
	}
	
	public static void main(String args[]){	
		
		ApplicationContextUtil.getAppContextInstance();
		Display display =(Display) ApplicationContextUtil.getBean("display");
		Shell shell = (Shell) ApplicationContextUtil.getBean("windowShell");  
		SWTCore.openShell(shell, display);
		SWTCore.setLocation(shell, display);
		
	}
	
	
}
